using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ghasam.Business.Services;
using Ghasam.Business.Domain.Entity;

public partial class GoodsPage : BasePage
{
    private IGoodsService goodsService;

    public IGoodsService GoodsService
    {
        get { return goodsService; }
        set { goodsService = value; }
    }
	

    public override string ActionName {
        get {
            return "vw_goods";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GetGoods(0, true);
        }
    }

    private void GetGoods(int pageNumber, bool setPageCount)
    {
        if (setPageCount)
            gvGoods.VirtualItemCount = goodsService.CountGoods();

        gvGoods.DataSource = goodsService.GetGoods(15, pageNumber);
        gvGoods.DataBind();
    }

    protected void gvGoods_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvGoods.PageIndex = e.NewPageIndex;
        GetGoods(e.NewPageIndex, false);
    }

    protected void btnSave_ServerClick(object sender, EventArgs e)
    {
        Goods goods = null;

        if (!String.IsNullOrEmpty(txtGoodsId.Text))
            goods = goodsService.GetGoods(Convert.ToInt32(txtGoodsId.Text));

        if (goods == null)
        {
            goods = new Goods();
            goods.CreateDate = DateTime.Now;
        }

        goods.IsDiscountable = cbIsDiscountable.Checked;
        goods.Name = txtGoodsName.Text;
        goods.UnitPrice = Convert.ToSingle(Mani.Common.UIHelper.RemoveFormat(txtUnitPrice.Text));

        goodsService.Persist(goods);
        GetGoods(gvGoods.PageIndex, false);
    }

    protected void gvGoods_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        int goodsId = Convert.ToInt32(gvGoods.DataKeys[e.NewSelectedIndex].Value);
        Goods goods = goodsService.GetGoods(goodsId);

        txtGoodsId.Text = goods.Id.ToString();
        txtGoodsName.Text = goods.Name;
        txtUnitPrice.Text = Mani.Common.UIHelper.FormatString( goods.UnitPrice );
        cbIsDiscountable.Checked = goods.IsDiscountable.Value;
        txtCreateDate.Text = Mani.Common.MyPersianDate.DateTimeToPersian( goods.CreateDate );
    }

    protected void gvGoods_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int goodsId = Convert.ToInt32(gvGoods.DataKeys[e.RowIndex].Value);
        Goods goods = goodsService.GetGoods(goodsId);
        goods.IsDeleted = true;
        goodsService.Persist(goods);
        GetGoods(0, true);
    }

    protected void btnClear_ServerClick(object sender, EventArgs e)
    {
        txtGoodsId.Text = "";
        txtGoodsName.Text = "";
        txtUnitPrice.Text = "";
        cbIsDiscountable.Checked = false;
        txtCreateDate.Text = "";
    }
}
